﻿using System;
using GNUGo.Net.CLI.CliControls;

namespace GNUGo.Net.CLI {
    public class CliGoForm : CliForm {
        private readonly CliLabel _turnsLabel;
        private readonly CliLabel _blackMovesLabel;
        private readonly CliLabel _whiteMovesLabel;
        private readonly CliLabel _messageLabel;
        private readonly CliPrompt _prompt;
        private readonly CliGoBoardControl _goBoard;
        private GoGame _goGame;

        public CliGoForm() {
            FullScreen = true;

            _turnsLabel = new CliLabel {
                Top = 2,
                Text = "Turn: xxx"
            };
            _blackMovesLabel = new CliLabel {
                Top = 6,
                Text = "Black (X): xxx"
            };
            _whiteMovesLabel = new CliLabel {
                Top = 7,
                Text = "White (O): xxx"
            };
            _goBoard = new CliGoBoardControl {
                Top = 0,
                Left = 0
            };
            _messageLabel = new CliLabel {
                Left = 0,
                Text = "Welcome to GNU Go.Net",
                Forecolor = ConsoleColor.Yellow
            };
            _prompt = new CliPrompt {
                Text = "???:",
                Left = 0
            };

            this.AddChild(_turnsLabel);
            this.AddChild(_blackMovesLabel);
            this.AddChild(_whiteMovesLabel);
            this.AddChild(_goBoard);
            this.AddChild(_messageLabel);
            this.AddChild(_prompt);
        }

        public GoGame Game {
            set {
                _goGame = value;
                _goBoard.Game = value;
                _goGame.MoveMade += _goGame_MoveMade;
                Invalidate();
            }
        }

        void _goGame_MoveMade(GoGame source, int x, int y, Player player) {
            Invalidate();
        }

        public CliLabel MessageLabel {
            get { return _messageLabel; }
        }

        public CliPrompt Prompt {
            get { return _prompt; }
        }

        protected override void OnPaint(object sender, PaintEventArgs args) {
            if (_goGame != null) {
                _turnsLabel.Left = _goBoard.Width + 3;
                _blackMovesLabel.Left = _goBoard.Width + 3;
                _whiteMovesLabel.Left = _goBoard.Width + 3;
                _prompt.Top = _goBoard.Top + _goBoard.Height + 2;
                _prompt.Width = args.Graphics.Width;
                if (_goGame.GameState == GameState.BlackToMove) {
                    _prompt.Text = "black: ";
                }
                else if (_goGame.GameState == GameState.WhiteToMove) {
                    _prompt.Text = "white: ";
                }
                else if (_goGame.GameState == GameState.GameOver) {
                    _prompt.Text = "game over: ";
                }
                _messageLabel.Top = _prompt.Top + _prompt.Height;
                _messageLabel.Width = args.Graphics.Width;
                _turnsLabel.Text = string.Format("Turn: {0,3}", _goGame.Turn);
                _blackMovesLabel.Text = string.Format("Black (X): {0,3}", _goGame.BlackCaptures);
                _whiteMovesLabel.Text = string.Format("White (O): {0,3}", _goGame.WhiteCaptures);
            }
            base.OnPaint(sender, args);
        }
    }
}
